package goodbaby.dklt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.classic.core.activity.TActivity;

import goodbaby.dklt.R;
import goodbaby.dklt.vedio.MovieRecorderView;

/**
 * Created by Jayden on 2016/5/4 11:34.
 *
 * @version V1.0
 * @Title:
 * @Description: ${todo}
 */
public class VedioRecordActivity extends TActivity {
    private Handler handler = new Handler() {
        public void handleMessage(Message paramAnonymousMessage) {
            VedioRecordActivity.this.finishActivity();
        }
    };
    private boolean isFinish = true;
    private MovieRecorderView mRecorderView;
    private Button mShootBtn;

    private void finishActivity() {
        if (isFinish) {
            this.mRecorderView.stop();
            Log.e("vedio", this.mRecorderView.getmVecordFile().toString());
            Log.e("vedioPath", this.mRecorderView.getmVecordFile().getPath().toString());
            Intent localIntent = new Intent(this, SendParentsCircleActivity.class);
            localIntent.putExtra("vedioPath", mRecorderView.getmVecordFile().getPath());
            localIntent.putExtra("msgType", 0);
            startActivity(localIntent);
            finish();
        }
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.movie_main);
        mRecorderView = (MovieRecorderView) findViewById(R.id.movieRecorderView);
        mShootBtn = (Button) findViewById(R.id.shoot_button);
        mShootBtn.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mRecorderView.record(new MovieRecorderView.OnRecordFinishListener() {

                        @Override
                        public void onRecordFinish() {
                            handler.sendEmptyMessage(1);
                        }
                    });
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (mRecorderView.getTimeCount() > 1)
                        handler.sendEmptyMessage(1);
                    else {
                        // if (mRecorderView.getVecordFile() != null)
                        //	mRecorderView.getVecordFile().delete();
                        mRecorderView.stop();
                        Toast.makeText(VedioRecordActivity.this, "视频录制时间太短",
                                Toast.LENGTH_SHORT).show();
                    }
                }
                return true;
            }
        });
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public void onResume() {
        super.onResume();
        this.isFinish = true;
    }

    protected void onSaveInstanceState(Bundle paramBundle) {
        super.onSaveInstanceState(paramBundle);
        this.isFinish = false;
        this.mRecorderView.stop();
    }
}
